package com.user.service.impl;

import java.util.Iterator;
import java.util.List;
import com.user.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.user.mapper.*;
import com.user.service.*;

/*业务层：MayuanService接口的实现类*/
@Service
public class MayuanServiceImpl implements MayuanService {
//	从IOC容器中注入mayuanMapper对象
	@Autowired
	private MayuanMapper mayuanMapper;

//	查询所有数据
	public List<Mayuan> findAll() {
		// TODO Auto-generated method stub
		return findAll(null);
	}

//	根据whereWrap条件查询符合条件的数据
	public List<Mayuan> findAll(Mayuan whereWrap) {
		// TODO Auto-generated method stub
		return findAll(whereWrap, true);
	}

//	根据whereWrap条件和查询模式查询符合条件的数据
	public List<Mayuan> findAll(Mayuan whereWrap, boolean isDistance) {
		// TODO Auto-generated method stub
		List<Mayuan> list = mayuanMapper.selectByExample(whereWrap);
		if (whereWrap!=null) {
			Iterator<Mayuan> iterator = list.iterator();
			while (iterator.hasNext()) {
				Mayuan mayuan = (Mayuan) iterator.next();
				if (whereWrap.getId() != null&&(!(whereWrap.getId()+"").equals(""))) {
					if (isDistance) {
						if (!(mayuan.getId() + "").equals(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(mayuan.getId() + "").contains(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getTopic() != null&&(!(whereWrap.getTopic()+"").equals(""))) {
					if (isDistance) {
						if (!(mayuan.getTopic() + "").equals(whereWrap.getTopic() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(mayuan.getTopic() + "").contains(whereWrap.getTopic() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getTopic_ans() != null&&(!(whereWrap.getTopic_ans()+"").equals(""))) {
					if (isDistance) {
						if (!(mayuan.getTopic_ans() + "").equals(whereWrap.getTopic_ans() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(mayuan.getTopic_ans() + "").contains(whereWrap.getTopic_ans() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getAnswer() != null&&(!(whereWrap.getAnswer()+"").equals(""))) {
					if (isDistance) {
						if (!(mayuan.getAnswer() + "").equals(whereWrap.getAnswer() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(mayuan.getAnswer() + "").contains(whereWrap.getAnswer() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getGrad() != null&&(!(whereWrap.getGrad()+"").equals(""))) {
					if (isDistance) {
						if (!(mayuan.getGrad() + "").equals(whereWrap.getGrad() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(mayuan.getGrad() + "").contains(whereWrap.getGrad() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getMultiple() != null&&(!(whereWrap.getMultiple()+"").equals(""))) {
					if (isDistance) {
						if (!(mayuan.getMultiple() + "").equals(whereWrap.getMultiple() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(mayuan.getMultiple() + "").contains(whereWrap.getMultiple() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
			} 
		}
		return list;
	}

//	根据主键查询符合条件的数据
	public Mayuan findById(Integer id) {
		// TODO Auto-generated method stub
		return mayuanMapper.selectByPrimaryKey(id);
	}

//	插入一条mayuan数据
	public int insert(Mayuan mayuan) {
		// TODO Auto-generated method stub
		return mayuanMapper.insertSelective(mayuan);
	}

//	根据主键更新一条mayuan数据
	public int updateById(Mayuan mayuan) {
		// TODO Auto-generated method stub
		return mayuanMapper.updateByPrimaryKey(mayuan);
	}

//	根据主键选择性更新一条mayuan数据
	public int updateByIdSelective(Mayuan mayuan) {
		// TODO Auto-generated method stub
		Mayuan dbMayuan = findById(mayuan.getId());
		if (mayuan.getId()==null) {
			mayuan.setId(dbMayuan.getId());
		}
		if (mayuan.getTopic()==null) {
			mayuan.setTopic(dbMayuan.getTopic());
		}
		if (mayuan.getTopic_ans()==null) {
			mayuan.setTopic_ans(dbMayuan.getTopic_ans());
		}
		if (mayuan.getAnswer()==null) {
			mayuan.setAnswer(dbMayuan.getAnswer());
		}
		if (mayuan.getGrad()==null) {
			mayuan.setGrad(dbMayuan.getGrad());
		}
		if (mayuan.getMultiple()==null) {
			mayuan.setMultiple(dbMayuan.getMultiple());
		}
		return mayuanMapper.updateByPrimaryKey(mayuan);
	}

//	根据主键删除一条mayuan数据
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return mayuanMapper.deleteByPrimaryKey(id);
	}

//	根据whereWrap条件删除符合条件的数据
	public int delete(Mayuan whereWrap) {
		// TODO Auto-generated method stub
		return delete(whereWrap, true);
	}

//	根据whereWrap条件和查询模式删除符合条件的数据
	public int delete(Mayuan whereWrap, boolean isDistance) {
		// TODO Auto-generated method stub
		List<Mayuan> list = findAll(whereWrap, isDistance);
		for (Mayuan mayuan : list) {
			mayuanMapper.deleteByPrimaryKey(mayuan.getId());
		}
		return 0;
	}

//	根据主键集合批量删除mayuan数据
	public int deleteByIds(List<Integer> list) {
		// TODO Auto-generated method stub
		for (Integer id : list) {
			deleteById(id);
		}
		return 0;
	}
}

